<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-28
 * Time: 23:33
 */

namespace app\api\controller\seller;
use app\common\controller\Api;

use app\common\model\Withdraw as WithdrawModel;
use app\common\model\Card;
use app\common\model\Member;
use app\common\model\Bank;

class Withdraw extends Api
{
    
    protected $set_rule = [
        'bank_id|银行id' => 'require',
        'bank_subbranch|开户支行' => 'require',
        'bank_username|开户名' => 'require',
        'bank_num|银行卡号' => 'require'
    ];
    
   
    
    
    public function index()
    {
        $params = paramForm();
    
        if(isset($params['action'])){
            switch ($params['action']){
                //发起提现的页面
                case 'add':
                    $card = Card::get(['user_id'=>$this->getUserId()]);
                    if(!$card){
                        error('请先设置提现信息');
                    }
                    success($card);
                    break;
                case 'post':
                    $params['user_id'] = $this->getUserId();
                    $this->save($params);
                    break;
//                case 'set':
//                    $card = Card::get(['user_id' => $this->getUserId()]);
//                    success($card);
//                    break;
                case 'bank':
                    success(Bank::all(['flag' => 1]));
                    break;
                case 'addSet':
                    $this->validate($params,$this->set_rule);
                    $isHas = Card::get(['user_id'=>$this->getUserId()]);
                    if($isHas){
                        $this->error('该用户已经有提现信息');
                    }
                    $bank = Bank::get($params['bank_id']);
                    $params['bank_name'] = $bank['name'];
                    $params['user_id'] = $this->getUserId();
                    $card = new Card();
                    $rt = $card->allowField(true)->save($params);
                    if($rt){
                        success($card);
                    }else{
                        error('绑定银行卡失败');
                    }
                    break;
                case 'upSet':
                    $this->validate($params,$this->set_rule);
                    $bank = Bank::get($params['bank_id']);
                    $params['bank_name'] = $bank['name'];
                    $card = new Card();
                    $rt = $card->allowField(true)->save($params,['id'=>$params['id']]);
                    if($rt){
                        success($card);
                    }else{
                        error('修改银行卡信息失败');
                    }
                    break;
                default:
                    break;
            }
            die;
        }
        
        $sellerId = $this->getUserId();
        $seller = Member::get($sellerId);
        $filter = ['user_id'=>$seller->id];
        $list = WithdrawModel::where($filter)->paginate(20);
    
        foreach ($list as $withdraw){
            $withdraw->status = $this->getStatus($withdraw->status);
        }
        success($list);
        
    }
    
    
    public function save($params){
        
        $rule = [
            'user_id|用户id'=>'require',
            'user_card_id|用户提现信息'=>'require',
            'number|提现金额'=>'require'
        ];
       
        $params['user_id'] = $this->getUserId();
        $this->validate($params,$rule);
        
        $seller = Member::get(['id'=>$this->getUserId()]);
    
        if($seller->cashing_money<(int)$params['number']){
            error('提现金额过大');
        }
    
        if((int)$params['number']<50){
            error('提现金额不能低于50');
        }
    
        $withdraw = new WithdrawModel();
        $rt = $withdraw->allowField(true)->save($params);
    
        //提现申请成功
        if($rt){
        
            //1.Ext表冻结金额增加
            $seller->freeze_money += $params['number'];
            //2.可提现金额减少
            $seller->cashing_money -= $params['number'];
            $rt2 = $seller->isUpdate()->save();
            
            api(1,'发起提现成功',null);
        }else{
            error('发起提现失败');
        }
        
        
    }

    
    protected function getStatus($status) {
        
        if($status==0)return '待审核';
        if($status==1)return '审核通过';
        if($status==-1)return '审核不通过';
        return '数据异常';//万一还有其他的呢
    }
    
}